/**
 * 
 * @param {number} everyPageNum 设置每页多少条数据
 * @param {boolean} flag 设置每页是否带表头，true带表头，false不带
 * 实现TR 样式中如出现  page-break-before: always; 则立即分页。
 */
function setPage( everyPageNum, flag) {

    let pageNum = 0;  //一共多少页
    let trNum = 0; //用于遍历的数
    let flog = false;
    let numb = 0;
    let originHtml = document.getElementsByClassName("layui-container-fluid")[0];//要操作的原始页面
    let j = 0;
    let dataNum= originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
    for (let i = 0; i < dataNum; i++) {
        numb++
        if (originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[i].style.pageBreakBefore == "always") {
            
            flog = true
            numb = 0
            pageNum++;
        } else if (numb > everyPageNum - 1) {
            flog = true;
            numb = 0;
            pageNum++
        } else if (i == dataNum - 1) {
            flog = true;
            numb = 0;
            pageNum++
        }
        if (flog) {
            flog = false;
            let divLayui = document.createElement("div");
            divLayui.style.pageBreakBefore = "always"
            let box = document.createElement("div");
            box.setAttribute("class", "box")
            let divRpt = document.createElement("div");
            divRpt.setAttribute("id", "rpt_header");
            divRpt.innerHTML = originHtml.querySelector("#rpt_header").innerHTML;
            divRpt.querySelector("#rpt_npage").innerHTML = pageNum
            divRpt.querySelector("#rpt_tpage").innerHTML = pageNum;
            box.appendChild(divRpt)
            let style = document.createElement("style")
            style.innerHTML = document.getElementsByClassName("box")[0].getElementsByTagName("style")[0].innerHTML;
            box.appendChild(style)
            divLayui.appendChild(box)
            let table = document.createElement("table");
            table.setAttribute("class", "tg");
            table.setAttribute("style", "undefined;table-layout: fixed; width: 1800px")
            let colgroup = document.createElement("colgroup");
            colgroup.innerHTML = originHtml.getElementsByTagName("colgroup")[0].innerHTML
            table.appendChild(colgroup)
            if (flag == true) {
                console.log("是否带有表头")
                let thead = document.createElement("thead");
                thead.innerHTML = originHtml.querySelector("thead").innerHTML
                table.appendChild(thead)
            }
            let tbody = document.createElement("tbody");
            for (; j <= i; j++) {
                let tr = document.createElement("tr")

                if (originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[trNum]) {
                    tr.innerHTML = originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[trNum].innerHTML;
                }
                trNum++;
                tbody.appendChild(tr);
            }
            table.appendChild(tbody)
            box.appendChild(table)
            let divFooter = document.createElement("div")
            divFooter.setAttribute("id", "rpt_footer")
            divFooter.innerHTML = originHtml.querySelector("#rpt_footer").innerHTML
            box.appendChild(divFooter)
            originHtml.parentNode.insertBefore(divLayui, originHtml)

        }

    }
    {
        let i = 0
        while (document.querySelectorAll("#rpt_npage")[i]) {
            document.querySelectorAll("#rpt_npage")[i].innerHTML = pageNum;
            i++;
        }

    }

    originHtml.remove();
}

setPage(10, true)